package com.jushu.storbox.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jushu.storbox.R;
import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.ShareFileSearchActivity;
import com.jushu.storbox.entities.SharedUser;


public class ShareFileSearchAdapter extends BaseAdapter {
	private List<SharedUser> adUserList;
	private Context context;
	private ViewHolder viewHolder;
	private ShareFileSearchActivity fileSearchActivity;

	public ShareFileSearchAdapter(Context context, List<SharedUser> adUserList) {
		this.adUserList = adUserList;
		this.context = context;
		this.fileSearchActivity = (ShareFileSearchActivity) context;
	}

	@Override
	public int getCount() {
		return adUserList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return adUserList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		if (arg1 == null) {
			arg1 = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.share_file_listview_item_layout, null);
			viewHolder = new ViewHolder();
			viewHolder.department = (TextView) arg1.findViewById(R.id.share_file_listview_item_department);
			viewHolder.share_file_listView_clean_rl = (RelativeLayout) arg1.findViewById(R.id.share_file_listView_clean_rl);
			viewHolder.share_file_listView_im_b = (ImageView) arg1.findViewById(R.id.share_file_listView_im);
			viewHolder.name = (TextView) arg1.findViewById(R.id.share_file_listview_item_name);
			viewHolder.loginID = (TextView) arg1.findViewById(R.id.share_file_listview_item_loginID);
			arg1.setTag(viewHolder);
		}
		viewHolder = (ViewHolder) arg1.getTag();
		String str = adUserList.get(arg0).getSharedUserName();

		if (adUserList.get(arg0).isShareStruts()) {
			viewHolder.share_file_listView_im_b.setImageDrawable(this.context.getResources().getDrawable(R.mipmap.user_head_image_b));
		} else {
			viewHolder.share_file_listView_im_b.setImageDrawable(this.context.getResources().getDrawable(R.mipmap.user_head_image));
		}
		viewHolder.loginID.setText(subStringEnd(str));
		viewHolder.share_file_listView_clean_rl.setOnClickListener(new CleanButtonClickListener(arg0));
		viewHolder.name.setText(subStringStart(str));
		viewHolder.department.setText(adUserList.get(arg0).getSharedDepartment());
		return arg1;
	}

	private String subStringStart(String str) {
		str.trim();
		int index = str.lastIndexOf(" ");
		if (index < 0) {
			return str;
		} else {
			str = str.substring(0, index);
			str = str.trim();
			return str;
		}
	}

	private String subStringEnd(String str) {
		str.trim();
		int index = str.lastIndexOf(" ");
		if (index < 0) {
			return "";
		} else {
			str = str.substring(index + 1, str.length());
			str = str.trim();
			return str;
		}
	}

	private class CleanButtonClickListener implements OnClickListener {
		private int itemID;

		public CleanButtonClickListener(int itemID) {
			this.itemID = itemID;
		}

		@Override
		public void onClick(View arg0) {
			if (!ShareDriveApplication.getInstance().isFastDoubleClick()) {
				fileSearchActivity.shareListOperating(this.itemID);
			}
		}
	}

	public static class ViewHolder {
		public TextView name, department, loginID;
		public ImageView share_file_listView_im_b;
		public RelativeLayout share_file_listView_clean_rl;
	}
}
